using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class PlayerMove : soldier
{
    public enum GamePlatformType{PC,PE}
    private Rigidbody2D rb;
    public GameObject[] TrooperPrefList;
    // 防止重复添加
    private HashSet<int> banSet=new HashSet<int> { };
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if(UdpPlatform.GetComponent<WebSocketDemo>().gameState>=2)
            PCMove();
    }

    void PCMove()
    {
        float moveX=UdpPlatform.GetComponent<WebSocketDemo>().positionX[PlayerId];
        float moveY=UdpPlatform.GetComponent<WebSocketDemo>().positionY[PlayerId];
        transform.position=new Vector3(moveX, moveY,0f);


        float angle=UdpPlatform.GetComponent<WebSocketDemo>().rotationZ[PlayerId];
        // 旋转角色
        Quaternion rotation = Quaternion.AngleAxis(-angle-90, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 50 * Time.fixedDeltaTime);

        // 武器库维护
        int[] bag=UdpPlatform.GetComponent<WebSocketDemo>().Bag[PlayerId];
        // 位置计算
        for(int i=0;i<bag.Length;i++)
        {
            float posX=(float)(i%5);
            float posY=(float)(i/5);
            if(bag[i]!=0&&banSet.Contains(i)==false)
            {
                GameObject prefabInstance = Instantiate(TrooperPrefList[bag[i]],transform.position, transform.rotation);

                prefabInstance.transform.SetParent(transform); 

                prefabInstance.GetComponent<soldier>().gridId=i;
                prefabInstance.GetComponent<soldier>().PlayerId=PlayerId;
                prefabInstance.GetComponent<soldier>().localPos=new Vector3(posX-2f, 2f-posY, 0);

                banSet.Add(i);
            }
            if(bag[i]==0&&banSet.Contains(i))
            {
                banSet.Remove(i);
                foreach (Transform child in transform)
                {
                    // 检查子物体上是否有名为“soldier”的组件
                    soldier soldierComponent = child.GetComponent<soldier>();
                    if (soldierComponent != null)
                    {
                        // 检查soldier组件的gridId是否为1
                        if (soldierComponent.gridId == i)
                        {
                            // 销毁子物体
                            Destroy(child.gameObject);
                        }
                    }
                }
            }

           
        }
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.name=="金币(Clone)"&&PlayerId==UdpPlatform.GetComponent<WebSocketDemo>().PlayerId)
        {
            UdpPlatform.GetComponent<WebSocketDemo>().PlayerCoin+=10;
            Destroy(other.gameObject);
        }
        
    }
    void OnTriggerStay2D(Collider2D other)
    {
        if(other.name=="金币(Clone)")
        {
            Destroy(other.gameObject);
            if(PlayerId==UdpPlatform.GetComponent<WebSocketDemo>().PlayerId)
                UdpPlatform.GetComponent<WebSocketDemo>().PlayerCoin+=10;
            
        }
        
    }
}
